<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="项目简称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup
                v-decorator="['projectShortName',validatorRules.projectShortName]"
                :trigger-change='true'
                org-fields='id,short_name'
                dest-fields='projectId,projectShortName'
                code='pm_project_select'
                @callback='popupCallback'
              />
              <input v-decorator="['projectId']" v-show='false'></input>
            </a-form-item>
          </a-col>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="回访人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['revisitPersion', validatorRules.revisitPersion]" placeholder="请输入回访人" disabled/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="被访人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['interviewee', validatorRules.interviewee]" placeholder="请输入被访人" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="被访人角色" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['intervieweeRole', validatorRules.intervieweeRole]" placeholder="请输入被访人角色" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="回访时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择回访时间" v-decorator="['revisitTime', validatorRules.revisitTime]" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="被访人联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contact', validatorRules.contact]" placeholder="请输入被访人联系方式" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="回访内容" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['revisitDes', validatorRules.revisitDes]" rows="4" placeholder="请输入回访内容" />
            </a-form-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2">-->
<!--              <a-textarea v-decorator="['remarks']" rows="4" placeholder="请输入备注" />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
        </a-row>
      </a-form>
    </j-form-container>
      <!-- 子表单区域 -->
<!--    <a-tabs v-model="activeKey" @change="handleChangeTabs">-->
<!--      <a-tab-pane tab="回访问题" :key="refKeys[0]" :forceRender="true">-->
<!--        <j-editable-table-->
<!--          :ref="refKeys[0]"-->
<!--          :loading="hfPmProjectRevisitQuestionsTable.loading"-->
<!--          :columns="hfPmProjectRevisitQuestionsTable.columns"-->
<!--          :dataSource="hfPmProjectRevisitQuestionsTable.dataSource"-->
<!--          :maxHeight="300"-->
<!--          :disabled="formDisabled"-->
<!--          :rowNumber="true"-->
<!--          :rowSelection="true"-->
<!--          :actionButton="true">-->
<!--          <template v-slot:action='props'>-->
<!--            <a @click='handleDelete(props)' :disabled='formDisabled'-->
<!--               style='width: 100%; display: inherit; text-align: center'-->
<!--            >-->
<!--              <a-icon type='close' style='color: red; font-size: 18px'></a-icon-->
<!--              >-->
<!--            </a>-->
<!--          </template>-->
<!--        </j-editable-table>-->

<!--      </a-tab-pane>-->
<!--    </a-tabs>-->
    <a-row v-if="showFlowSubmitButton" style="text-align: center;width: 100%;margin-top: 16px;"><a-button @click="handleOk">提 交</a-button></a-row>
  </a-spin>
</template>

<script>

  import pick from 'lodash.pick'
  import { getAction, httpAction } from '@/api/manage'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'HfPmProjectRevisitRecordsForm',
    mixins: [JEditableTableMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          revisitPersion: {
            rules: [
              { required: true, message: '请输入回访人!'},
            ]
          },
          interviewee: {
            rules: [
              { required: true, message: '请输入被访人!'},
            ]
          },
          intervieweeRole: {
            rules: [
              { required: true, message: '请输入被访人角色!'},
            ]
          },
          revisitTime: {
            rules: [
              { required: true, message: '请输入回访时间!'},
            ]
          },
          revisitDes: {
            rules: [
              { required: true, message: '请输入回访内容!'},
            ]
          },
          contact: {
            rules: [
              { required: true, message: '请输入被访人联系方式!'},
            ]
          },
        },
        refKeys: ['hfPmProjectRevisitQuestions', ],
        tableKeys:['hfPmProjectRevisitQuestions', ],
        activeKey: 'hfPmProjectRevisitQuestions',
        // hf_pm_project_revisit_questions
        hfPmProjectRevisitQuestionsTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '问题类型',
              key: 'questionType',
              type: FormTypes.select,
              dictCode: 'project_question_type',
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              // validateRules: [{ required: true, message: '${title}不能为空' }]
            },
            {
              title: '问题描述',
              key: 'questionDes',
              type: FormTypes.input_pop,
              width:"500px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            // {
            //   title: '解决方式',
            //   key: 'solution',
            //   type: FormTypes.input_pop,
            //   width:"250px",
            //   placeholder: '请输入${title}',
            //   defaultValue:'',
            // },
            // {
            //   title: '状态',
            //   key: 'questionState',
            //   type: FormTypes.select,
            //   width:"150px",
            //   placeholder: '请输入${title}',
            //   dictCode: 'project_question_state',
            //   defaultValue:'1',
            // },
            {
              title: '责任人',
              key: 'dutyPersionId',
              type: FormTypes.sel_user,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            // {
            //   title: '处理结果',
            //   key: 'dealResult',
            //   type: FormTypes.input,
            //   width:"200px",
            //   placeholder: '请输入${title}',
            //   defaultValue:'',
            // },
            // {
            //   title: '问题处理人',
            //   key: 'dealPersionId',
            //   type: FormTypes.input,
            //   width:"200px",
            //   placeholder: '请输入${title}',
            //   defaultValue:'',
            // },
            // {
            //   title: '处理时间',
            //   key: 'dealTime',
            //   type: FormTypes.date,
            //   width:"200px",
            //   placeholder: '请输入${title}',
            //   defaultValue:'',
            // },
            // {
            //   title: '备注',
            //   key: 'remarks',
            //   type: FormTypes.input_pop,
            //   width:"200px",
            //   placeholder: '请输入${title}',
            //   defaultValue:'',
            // },
            {
              title: '操作',
              key: 'action',
              width: '5%',
              type: FormTypes.slot,
              slotName: 'action'
            }
          ]
        },
        url: {
          add: "/pm/hfPmProjectRevisitRecords/add",
          edit: "/pm/hfPmProjectRevisitRecords/edit",
          queryById: "/pm/hfPmProjectRevisitRecords/queryById",
          hfPmProjectRevisitQuestions: {
            list: '/pm/hfPmProjectRevisitRecords/queryHfPmProjectRevisitQuestionsByMainId'
          },
        }
      }
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      handleDelete(props) {
        let { rowId, target } = props
        target.removeRows(rowId)
        // console.log(props.getValue())
        // this.getValues()
        this.dataSource()
      },
      addBefore(){
        this.form.resetFields()
        this.hfPmProjectRevisitQuestionsTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        console.log("values>================="+values)
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'projectId','projectShortName','revisitPersion','interviewee','intervieweeRole','revisitTime','contact','revisitDes')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.hfPmProjectRevisitQuestions.list, params, this.hfPmProjectRevisitQuestionsTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          hfPmProjectRevisitQuestionsList: allValues.tablesValue[0].values,
        }
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          })
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'projectId','projectShortName','revisitPersion','interviewee','intervieweeRole','revisitTime','contact','revisitDes'))
     },

    }
  }
</script>

<style scoped>
</style>